package com.sofwin.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.system.dto.ResourceDto;
import com.sofwin.system.mapper.SysRoleMapper;
import com.sofwin.system.mapper.SysRoleResourceMapper;
import com.sofwin.system.pojo.SysRole;
import com.sofwin.system.pojo.SysRoleExample;
import com.sofwin.system.pojo.SysRoleResourceExample;
import com.sofwin.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @packageName: com.sofwin.system.service.impl
 * @user: andyliu
 * @date: 2021/7/14 16:21
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper mapper;
    @Autowired
    private SysRoleResourceMapper roleResourceMapper;
    @Override
    public PageInfo<SysRole> queryRoleByPage(SysRole role, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SysRoleExample ex = new SysRoleExample();
        SysRoleExample.Criteria criteria = ex.createCriteria();
        if(role!=null){
            if(role.getRoleName()!=null && !"".equals(role.getRoleName())){
                criteria.andRoleNameLike("%"+role.getRoleName()+"%");
            }
            if(role.getRoleCode()!=null && !"".equals(role.getRoleCode())){
                criteria.andRoleCodeEqualTo(role.getRoleCode());
            }
            if(role.getStatus()!=null && role.getStatus()!=-1){
                criteria.andStatusEqualTo(role.getStatus());
            }
        }
        List<SysRole> roles = mapper.selectByExample(ex);
        return new PageInfo(roles);
    }

    @Override
    public boolean saveOrUpdateRole(SysRole role) {
        if(role.getRoleId()!=null){
            return mapper.updateByPrimaryKeySelective(role)==1?true:false;
        }
        return mapper.insertSelective(role)==1?true:false;
    }

    @Override
    public SysRole queryRoleById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removleByIds(Integer[] ids) {
        int flag=0;
        for(Integer id:ids){
            if(removeById(id)){
                flag++;
            }
        }
        return flag==ids.length?true:false;
    }

    @Override
    @Transactional
    public boolean saveRolePermission(Integer roleId, Integer[] ids) {
        SysRoleResourceExample ex = new SysRoleResourceExample();
        SysRoleResourceExample.Criteria criteria = ex.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        roleResourceMapper.deleteByExample(ex);
        int num = mapper.saveRolePermission(roleId, ids);
        return num==ids.length?true:false;
    }

    @Override
    public List<SysRole> queryAllRoles() {
        return mapper.selectByExample(null);
    }

    @Override
    public List<ResourceDto> queryResource(Integer roleId) {
        return mapper.queryResource(roleId);
    }
}
